package patrol.mobile.com.mobilepatrol.utils;

import android.graphics.Color;
import android.util.Log;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

import patrol.mobile.com.mobilepatrol.bean.BarDataBean;

/**
 * Created by z on 2018/9/17.
 * Des:柱状图工具类
 */

public class BarChartUtil {
    public static void setBarChart(BarChart barChart, List<BarDataBean> barDataBeans, int color, String label) {
        YAxis leftAxis;             //左侧Y轴
        YAxis rightAxis;            //右侧Y轴
        XAxis xAxis;                //X轴
        Legend legend;              //图例
        LimitLine limitLine;        //限制线
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(true);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(true);
        //设置动画效果
        barChart.animateY(1000, Easing.EasingOption.Linear);
        barChart.animateX(1000, Easing.EasingOption.Linear);
        Description description = new Description();
        description.setText("");
        barChart.setDescription(description);
        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        xAxis.setAvoidFirstLastClipping(true);
        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
        initBarDataSet(barChart, barDataBeans, color, label, xAxis);
    }

    public static void setBarChart(BarChart barChart, List<BarDataBean> barDataBeans, int[] color, String label) {
        YAxis leftAxis;             //左侧Y轴
        YAxis rightAxis;            //右侧Y轴
        XAxis xAxis;                //X轴
        Legend legend;              //图例
        LimitLine limitLine;        //限制线
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(true);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(true);
        //设置动画效果
        barChart.animateY(1000, Easing.EasingOption.Linear);
        barChart.animateX(1000, Easing.EasingOption.Linear);
        Description description = new Description();
        description.setText("");
        barChart.setDescription(description);
        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        xAxis.setAvoidFirstLastClipping(true);
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
        initBarDataSet(barChart, barDataBeans, color, label, xAxis);
    }

    public static void setBarChartchange(BarChart barChart, List<BarDataBean> barDataBeans, int[] color, String label) {
        YAxis leftAxis;             //左侧Y轴
        YAxis rightAxis;            //右侧Y轴
        XAxis xAxis;                //X轴
        Legend legend;              //图例
        LimitLine limitLine;        //限制线
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(true);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(true);
        //设置动画效果
        barChart.animateY(1000, Easing.EasingOption.Linear);
        barChart.animateX(1000, Easing.EasingOption.Linear);
        Description description = new Description();
        description.setText("");
        barChart.setDescription(description);
        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        xAxis.setAvoidFirstLastClipping(true);
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //保证Y轴从0开始，不然会上移一点
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
        initBarDataSet(barChart, barDataBeans, color, label, xAxis);
    }

    /**
     * 柱状图始化设置 一个BarDataSet 代表一列柱状图
     * <p>
     * barDataSet 柱状图
     * color      柱状图颜色
     */
    private static void initBarDataSet(BarChart barChart, final List<BarDataBean> barDataBeans, int color, String label, XAxis xAxis) {
        List<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < barDataBeans.size(); i++) {
            BarEntry entry = new BarEntry(i, (float) barDataBeans.get(i).getfValue());
            entries.add(entry);
        }

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Log.e("~~~~~~lalal~~~~~~", "__________" + value);
                return barDataBeans.get((int) value % barDataBeans.size()).getxName();
            }
        });
        BarDataSet barDataSet = new BarDataSet(entries, label);
        barDataSet.setColor(color);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(false);
        barDataSet.setValueTextSize(10f);
        barDataSet.setValueTextColor(color);
        barChart.setData(new BarData(barDataSet));
    }

    /**
     * 柱状图始化设置 一个BarDataSet 代表一列柱状图
     * <p>
     * barDataSet 柱状图
     * color      柱状图颜色
     */
    private static void initBarDataSet(BarChart barChart, final List<BarDataBean> barDataBeans, int[] colors, String label, XAxis xAxis) {
        List<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < barDataBeans.size(); i++) {
            BarEntry entry = new BarEntry(i, (float) barDataBeans.get(i).getfValue());
            entries.add(entry);
        }

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                Log.e("~~~~~~lalal~~~~~~", "__________" + value);
                return barDataBeans.get((int) value % barDataBeans.size()).getxName();
            }
        });
        BarDataSet barDataSet = new BarDataSet(entries, "BarDataSet");
        barDataSet.setColors(colors, 255);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(false);
//        barDataSet.setValueTextSize(10f);
//        barDataSet.setValueTextColor(color);
        BarData barData = new BarData(barDataSet);
        barChart.setFitBars(true); //使x轴完全适合所有条形
        barChart.setData(barData);
    }
}
